package com.td.evo.obj.factory;

import com.krakers.evo.log.EvolutionLogic;
import com.krakers.evo.obj.DNA;
import com.krakers.evo.obj.factory.PopulationFactory;
import com.td.core.obj.TDMap;

public class TDPathFactory extends PopulationFactory implements TDMap {

	private static final int X = BLD_X/2;
	private static final int Y = BLD_Y/2;
	
	@Override
	public DNA createSpecimen() {
		DNA dna = new DNA(225);
		
		//fill all
		for(int i = 0; i < Y; i++) {
			for(int j = 0; j < X; j++) {
				dna.setGene(X * i + j, 1);
			}
		}
		
		//make path
		int z1 = 0;
		int z2 = EvolutionLogic.r.nextInt(X); //generated for first row
		dna.setGene(z2, 0);
		
		for(int i = 1; i < Y; i+=2) {
			z1 = z2;
			z2 = EvolutionLogic.r.nextInt(X);
			if(i != Y-1) {
				dna.setGene(X * (i+1) + z2, 0);
				for(int j = Math.min(z1, z2); j <= Math.max(z1, z2); j++) {
					dna.setGene(X * i + j, 0);
				}
			} else {
				dna.setGene(X * i + z2, 0);
			}
			
		}
		
		return dna;
	}

	public DNA createSpecimen(String str) {
		DNA dna = new DNA(225);
		
		for(int i = 0; i < dna.getSize(); i++) {
			int v = Integer.parseInt(str.substring(0, 1));
			str = str.substring(2);
			dna.setGene(i, v);
		}
		
		return dna;
	}
}
